﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using SomethingSpacialClassLib.Web;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Printing;
using System.IO;
using Elisy.Barcode;
using System.Text;

namespace SomethingSpacial.Views.MixItUp
{
    public partial class PrintBadgePage : ChildWindow
    {


        private CaptureSource CapSrc = new CaptureSource();
        private VideoBrush MyVideoBrush = new VideoBrush();
        private bool VideoIsPlaying = false;

        public PrintBadgePage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(PrintBadgePage_Loaded);
        }

        void PrintBadgePage_Loaded(object sender, RoutedEventArgs e)
        {

            StartCamBtn.Click += new RoutedEventHandler(StartCamBtn_Click);
            btnPrintPreview.Click += new RoutedEventHandler(btnPrintPreview_Click);
            btnPrint.Click += new RoutedEventHandler(btnPrint_Click);
            btnPrintCancel.Click += new RoutedEventHandler(btnPrintCancel_Click);

            var ctx = new SomethingSpacialContext();
            EntityQuery<Event> latestEventQuery = ctx.GetEventLatestQuery();
            var loadOperation = ctx.Load(latestEventQuery);


            if (!loadOperation.HasError)
                loadOperation.Completed += (s, ev) =>
                {
                    Event evt = loadOperation.Entities.SingleOrDefault();

                    if (evt.EVT_Description.Length > 350)
                    {
                        evt.EVT_Description = evt.EVT_Description.Substring(0, 350) + "...";
                    }

                    badgePageCanvas.DataContext=evt; 


                    EntityQuery<SomethingSpacialClassLib.Web.Location> eventLocation = ctx.GetLocationsQuery();
                    var loadLocation = ctx.Load(eventLocation);

                    loadLocation.Completed += (locSender, args) =>
                    {
                        SomethingSpacialClassLib.Web.Location loc = loadLocation.Entities.Where(l => l.LOC_Id == evt.EVT_Location).SingleOrDefault();
                    };

                    textBlockAttendeeName.Text = "";// AppServices.WebContext.Current.User.FriendlyName;
                    BarcodeEngine be = new BarcodeEngine();
                    //textBlockAttendeeId.Text = be.Code39(AppServices.WebContext.Current.User.GetIdentity().ToString().ToUpper());

                };
        }

        void btnPrintCancel_Click(object sender, RoutedEventArgs e)
        {
            printGridCanvas.Visibility = Visibility.Collapsed;
        }

        void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            preparePrintData();

        }


        void btnPrint_Click(object sender, RoutedEventArgs e)
        {

            PrintDocument badge2Print = new PrintDocument();


             badge2Print.EndPrint += (owner, args) =>
             {
                 printGridCanvas.Visibility = Visibility.Collapsed;             
             };

            badge2Print.PrintPage += (owner, args) => { args.PageVisual = printGrid; };
            badge2Print.Print("EventAttendeeBadge");
        }


        void preparePrintData()
        {

            textBlockAttendeeIdPrint.Text = textBlockAttendeeId.Text;
            textBlockAttendeeNamePrint.Text = textBlockAttendeeName.Text;

            imageRectanglePrint.Fill = WebcamRectangle.Fill;
            printGridCanvas.Visibility = Visibility.Visible;
        }



        void StartCamBtn_Click(object sender, RoutedEventArgs e)
        {
            if (!VideoIsPlaying)
            {
                VideoIsPlaying = true;
                if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
                {
                    MyVideoBrush.SetSource(CapSrc);
                    WebcamRectangle.Fill = MyVideoBrush;
                    CapSrc.Start();
                    StartCamBtn.Content = "Take Snapshot";
                }
            }
            else
            {
                VideoIsPlaying = false;
                CapSrc.Stop();
                StartCamBtn.Content = "Start Webcam";
            }

        }


 

    }
}

